package com.xuxiao.controller;

import com.xuxiao.common.CardAndBalance;
import com.xuxiao.common.CartAndName;
import com.xuxiao.common.NameAndCardID;
import com.xuxiao.common.Result;
import com.xuxiao.entrty.CardInfo;
import com.xuxiao.service.CardInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/card")
public class CardInfoController {
    @Autowired
    private CardInfoService cardInfoService;

    @PostMapping
    public Result addCard(@RequestBody CardInfo cardInfo) {
        cardInfoService.addCard(cardInfo);
        log.info("开户：：{}", cardInfo);
        return Result.success();
    }

    @GetMapping
    public Result SelectAll() {
        List<CartAndName> cartAndNames = cardInfoService.SelectAll();
        return Result.success(cartAndNames);
    }

    @GetMapping("/{id}")
    public Result SelectByID(@PathVariable String id) {
        List<CardInfo> cardInfo = cardInfoService.SelectByID(id);
        return Result.success(cardInfo);
    }

    @DeleteMapping("/{ids}")
    public Result DeleteCardByID(@PathVariable List<Integer> ids) {
        cardInfoService.DeleteCardByID(ids);
        return Result.success();
    }

    @PutMapping
    public Result updateCardBy(@RequestBody CardInfo cardInfo) {
        cardInfoService.updateCardBy(cardInfo);
        return Result.success();
    }

    @GetMapping("/user/{id}")
    public Result getCardByUserID(@PathVariable String id) {
        List<CardInfo> cardInfoList = cardInfoService.getCardByUserID(id);
        return Result.success(cardInfoList);
    }

    @PutMapping("/plus")
    public Result plusByCard(@RequestBody CardInfo cardInfo) {
        log.info("存款界面，{}", cardInfo);
        cardInfoService.plusByCard(cardInfo);
        return Result.success();
    }

    @GetMapping("/allblance/{id}")
    public Result getAllCartBalance(@PathVariable String id) {
        log.info("存款界面，{}", id);
        List<CardAndBalance> cardAndBalances = cardInfoService.getAllCartBalance(id);
        return Result.success(cardAndBalances);
    }

    @GetMapping("/nameandcardid/{id}")
    public Result getNameAndCardID(@PathVariable String id) {
        log.info("返回姓名和卡号；{}", id);
        List<NameAndCardID> nameAndCardIDS = cardInfoService.getNameAndCardID(id);
        return Result.success(nameAndCardIDS);
    }

    @PostMapping("/iscorrect")
    public Result IsCorrect(@RequestBody CardInfo cardInfo) {
        int result = cardInfoService.IsCorrect(cardInfo);
        if (result == 0) {
            return Result.error("账号密码错误");
        } else {
            return Result.success();
        }
    }
}

